/*
* MIT License
*
* Copyright (c) 2017 wen.gu <454727014@qq.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/


#include "tiny_ring_buffer.h"
#include "general_error.h"

#define LOG_TAG "RBTest"
#include "vos_log.h"
///////////////////////////////////////////////////////////////////////////////
#define RING_BUF_SIZE (16 * 1024)
#define REQ_BUF_SIZE  (1024)
#define ALIGN_SIZE    (1024)

///////////////////////////////////////////////////////////////////////////////
static GU08 g_buf[RING_BUF_SIZE] = { 0 };


///////////////////////////////////////////////////////////////////////////////


void ring_buffer_test()
{
    vosLogEnableTimestamp(GTRUE);

    GU32 i = 0;
    GS32 ret = G_OK;
    GU32 do_flag = 0;
    GU32 test_num = 0;
    GU08 *temp_buf = NULL;
    GU08 *last_buf = NULL;
    tiny_ring_buffer_t* trb = NULL;
    //create a ring buffer  instance.

    ret = tfRingBufferCreate(&trb, RING_BUF_SIZE, NULL);


    LOGD("strat test tiny ring buffer...\n");

    LOGD("test(%d), supply memory by ring buffer self(malloc)...\n", test_num);
    do
    {
        do_flag = 0;

        if (!trb)
        {
            LOGE("Error: create a ring buffer instance failed... \n");
            ret = G_ErrorInsufficientResources;
            break;
        }

        LOGD("<<======================================>> \n");
        ret = trb->size;
        LOGD("create size(%d), reallysize(%d)\n", RING_BUF_SIZE, ret);
        if (ret != RING_BUF_SIZE)
        {
            LOGE("ring buffer size invalid(%d), request(%d)\n", trb->size, RING_BUF_SIZE);
            ret = G_ErrorUndefined;
            break;
        }
        else
        {
            ret = G_OK;
        }

        for (i = 0; i < RING_BUF_SIZE / REQ_BUF_SIZE; i++)
        {
            temp_buf = tfRingBufferAlloc(trb, REQ_BUF_SIZE);
            LOGD("alloc buffer(%p), size(%d)\n", temp_buf, REQ_BUF_SIZE);
            if (temp_buf == NULL)
            {
                LOGE("alloc buffer failed(0x%08x)\n", trb->last_error);
                break;
            }

            if ((i % 2))
            {
                tfRingBufferFree(trb, last_buf, REQ_BUF_SIZE * 2);
                LOGD("free buffer(%p), size(%d)\n", last_buf, REQ_BUF_SIZE * 2);
                if (trb->last_error != G_OK)
                {
                    LOGE("free buffer failed(0x%08x)\n", trb->last_error);
                    break;
                }

                last_buf = NULL;
            }
            else
            {
                last_buf = temp_buf;
            }
        }

        //todo test

        LOGD("<<======================================>> \n");
        test_num++;

        if (test_num < 2)
        {
            tfRingBufferDestroy(trb);
            trb = NULL;

            //test external provide buffer
            ret = tfRingBufferCreate(&trb, RING_BUF_SIZE, g_buf);
            LOGD("test(%d), supply memory by external buffer...\n", test_num);
            do_flag = 1;
            continue;
        }

    } while (do_flag);

    if (trb)
    {
        tfRingBufferDestroy(trb);
    }

    LOGD("end of test tiny ring buffer...\n");
}